<?php

namespace App\Helpers\Lib;

use Illuminate\Support\Facades\Redis;

class RedisService
{
    protected static $sk = 'user_list_other';

    protected static $prefix = 'p';

    /**
     * unserialize()
     * 将数据添加到hash中去
     * @return void
     */
    public static function hSet($user_id,$user_info){
        Redis::connection()->client()->hSet(self::$sk,self::$prefix.$user_id,serialize($user_info));
    }


    /**
     * @param $user_id
     * @param $user_info
     * @return void
     * @throws \RedisException
     */
    public static function hUpdate($user_id,$user_info){
        //删除掉旧的数据重新添加一条
        Redis::connection()->client()->hDel(self::$sk,self::$prefix.$user_id);
        Redis::connection()->client()->hSet(self::$sk,self::$prefix.$user_id,serialize($user_info));
    }

    public static function hGet($user_id){
        $res = Redis::connection()->client()->hGet(self::$sk,self::$prefix.$user_id);
        return unserialize($res);
    }

    /**
     * @return void
     */
    public static function delAll(){
        Redis::connection()->client()->del(self::$sk);
    }


    /**
     * @return array
     * @throws \RedisException
     */
    public static function getAlls(){
        $res = Redis::connection()->client()->hGetAll(self::$sk);
        $resData = [];
        foreach ($res as $k=>$v){
            //var_dump($k);
            $resData[] = unserialize($v);
        }
        return $resData;
    }
}
